﻿using System;
using System.ComponentModel.DataAnnotations;

using mvcForum.Core;
using mvcForum.Web.Attributes;

namespace mvcForum.Web.ViewModels {

	public class PostReportViewModel {

		public PostReportViewModel() { }

		public PostReportViewModel(PostReport pr) {
			this.Populate(pr);
		}

		public void Populate(PostReport report) {
			this.Id = report.Id;
			this.Reason = report.Reason;
			//this.Post = new MessageViewModel(report.Post) { Topic = new TopicViewModel(report.Post.Topic, 1) };
			this.Resolved = report.Resolved;
			this.Feedback = report.Feedback;
			this.Timestamp = report.Timestamp;
			this.ReporterId = report.ReportedBy.Id;
			this.ReporterName = report.ReportedBy.Name;
			if (report.ResolvedBy != null && report.ResolvedTimestamp.HasValue) {
				this.ResolvedById = report.ResolvedBy.Id;
				this.ResolvedByName = report.ResolvedBy.Name;
				this.ResolvedTimestamp = report.ResolvedTimestamp;
			}

			this.IsTopic = false;
			if (report.Post.Position == 0) {
				this.IsTopic = true;
				this.Title = report.Post.Topic.Title;
				this.TopicFlag = report.Post.Topic.Flag;
				this.TopicType = report.Post.Topic.Type;
				this.TopicId = report.Post.Topic.Id;
				this.Author = report.Post.Topic.Author.Name;
				this.AuthorId = report.Post.Topic.Author.Id;
				this.Posted = report.Post.Posted;
			}
			else {
				this.Subject = report.Post.Subject;
				this.PostFlag = report.Post.Flag;
				this.PostId = report.Post.Id;
				this.Author = report.Post.Author.Name;
				this.AuthorId = report.Post.Author.Id;
				this.Posted = report.Post.Posted;
			}
			this.Content = report.Post.Body;
			this.LastEdited = report.Post.Edited;
			if (this.LastEdited.HasValue) {
				this.ModeratorChanged = report.Post.ModeratorChanged;
			}
		}

		public Int32 Id { get; set; }
		public String Reason { get; private set; }
		public Boolean Feedback { get; private set; }
		public DateTimeOffset Timestamp { get; private set; }

		public String ReporterName { get; private set; }
		public Int32 ReporterId { get; private set; }

		[LocalizedDisplay("mvcForum.Web", "PostReportViewModel.Resolved")]
		public Boolean Resolved { get; set; }
		public String ResolvedByName { get; private set; }
		public Int32? ResolvedById { get; private set; }
		public DateTimeOffset? ResolvedTimestamp { get; private set; }

		// Topic fields
		public Int32 TopicId { get; private set; }
		[LocalizedDisplay("mvcForum.Web", "PostReportViewModel.Title")]
		public String Title { get; set; }
		[LocalizedDisplay("mvcForum.Web", "PostReportViewModel.TopicFlag")]
		public TopicFlag TopicFlag { get; set; }
		[LocalizedDisplay("mvcForum.Web", "PostReportViewModel.TopicType")]
		public TopicType TopicType { get; set; }

		// Post fields
		public Int32 PostId { get; private set; }
		[LocalizedDisplay("mvcForum.Web", "PostReportViewModel.Subject")]
		public String Subject { get; set; }
		[LocalizedDisplay("mvcForum.Web", "PostReportViewModel.PostFlag")]
		public PostFlag PostFlag { get; set; }

		// Common fields
		[Required]
		[LocalizedDisplay("mvcForum.Web", "PostReportViewModel.Content")]
		public String Content { get; set; }
		[LocalizedDisplay("mvcForum.Web", "PostReportViewModel.ChangeReason")]
		public String ChangeReason { get; set; }
		public Boolean IsTopic { get; private set; }
		public Int32 AuthorId { get; private set; }
		public String Author { get; private set; }
		public DateTime Posted { get; private set; }
		public DateTime? LastEdited { get; private set; }
		public Boolean ModeratorChanged { get; private set; }
		//public MessageViewModel Post { get; private set; }
	}
}